package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.ChooseCourse;
import com.grad.why.studentsystem.domain.Course;
import com.grad.why.studentsystem.domain.current.CurrentCourse;
import com.grad.why.studentsystem.domain.current.CurrentProject;
import com.grad.why.studentsystem.service.ChooseCourseService;
import com.grad.why.studentsystem.service.CourseService;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentManageSystem")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private ChooseCourseService chooseCourseService;

    @Autowired
    private OptionparamsService optionparamsService;


    @RequestMapping("/getCourseInfos")
    @ResponseBody
    public Object getProjectInfos(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            List<CurrentCourse> rows = courseService.getCourseList(offset, limit, search);
            long total = courseService.getCourseListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addCourseInfo")
    @ResponseBody
    public String addCourseInfo(Course course){
        try{
            int maxCourseId = courseService.getMaxCourseId() + 1;
            course.setCourseid(maxCourseId);
            String coursename = course.getCoursename();
//          if(courseService.checkCourseExistByCourseName(coursename)){
                int n = courseService.addCourseInfoByEntity(course);
//          }
            if(n>0){
                return "OK";
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteCourseinfo/{ids}")
    @ResponseBody
    public String deleteCourseInfoByIds(@PathVariable("ids") String ids){
        try{
            String[] idArray = ids.split("-");
            for(String s : idArray){
                int n = courseService.deleteCourseInfoById(Integer.parseInt(s));
                if(n <= 0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateCourseInfo")
    @ResponseBody
    public String updateCourseInfo(Course course){
        try{
            int rows = courseService.updateCourseInfoByEntity(course);
            String courseState = course.getCoursestate();
            List<ChooseCourse> chooseCourseByCid = chooseCourseService.getChooseCourseByCid(course.getCourseid());
            for(int i = 0;i<chooseCourseByCid.size();i++){
                chooseCourseByCid.get(i).setState(courseState);
                int n = chooseCourseService.updateChooseCourseInfoByEntity(chooseCourseByCid.get(i));
                if(n <= 0){
                    return null;
                }
            }
            if(rows <= 0){
                return null;
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getCourseStateByCid/{courseId}")
    @ResponseBody
    public String getCourseStateByCid(@PathVariable("courseId") String courseId){
        try{
            return courseService.getCourseStateByCid(Integer.parseInt(courseId));
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadCourseInfos")
    @ResponseBody
    public String uploadStudentInfos(@RequestParam("file") MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadCourseInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadCourseInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = courseService.addCourseInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
            //上传学生信息逻辑
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
